home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_085 / rawio / raw.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  90 lines

  1. /* 
  2.  *    raw.c
  3.  *
  4.  *    This is a routine for setting a given stream to raw or cooked mode.
  5.  * This is useful when you are using Lattice C to produce programs that
  6.  * want to read single characters with the "getch()" or "fgetc" call.
  7.  *
  8.  * Written : 18-Jun-87 By Chuck McManis. 
  9.  *          If you use it I would appreciate credit for it somewhere.
  10.  */
  11. #include <exec/types.h>
  12. #include <libraries/dos.h>
  13. #include <libraries/dosextens.h>
  14. #include <stdio.h>
  15. #include <ios1.h>
  16. #include <error.h>
  17.  
  18. /* New Packet in 1.2 */
  19. #define ACTION_SCREEN_MODE    994L
  20.  
  21. extern    int    errno;        /* The error variable */
  22.  
  23. /*
  24.  * Function raw() - Convert the specified file pointer to 'raw' mode. This
  25.  * only works on TTY's and essentially keeps DOS from translating keys for
  26.  * you, also (BIG WIN) it means getch() will return immediately rather than
  27.  * wait for a return. You lose editing features though.
  28.  */
  29. long
  30. raw(fp)
  31.  
  32. FILE *fp;
  33.  
  34. {
  35.   struct MsgPort     *mp; /* The File Handle message port */
  36.   struct FileHandle     *afh;
  37.   struct UFB         *ufb;
  38.   long            Arg[1],res;
  39.  
  40.   ufb = (struct UFB *) chkufb(fileno(fp));  /* Step one, get the file handle */
  41.   afh = (struct FileHandle *)(ufb->ufbfh); 
  42.  
  43.   if (!IsInteractive(afh)) {    /* Step two, check to see if it's a console */
  44.     errno = ENOTTY;
  45.     return(-1);
  46.   }
  47.                               /* Step three, get it's message port. */
  48.   mp  = ((struct FileHandle *)(BADDR(afh)))->fh_Type;
  49.   Arg[0] = -1L;
  50.   res = SendPacket(mp,ACTION_SCREEN_MODE,Arg,1); /* Put it in RAW: mode */
  51.   if (res == 0) {
  52.     errno = ENXIO;
  53.     return(-1);
  54.   }
  55.   return(0);
  56. }
  57.  
  58. /*
  59.  * Function - cooked() this function returns the designate file pointer to
  60.  * it's normal, wait for a <CR> mode. This is exactly like raw() except that
  61.  * it sends a 0 to the console to make it back into a CON: from a RAW:
  62.  */
  63.  
  64. long
  65. cooked(fp)
  66.  
  67. FILE *fp;
  68.  
  69. {
  70.   struct MsgPort     *mp; /* The File Handle message port */
  71.   struct FileHandle     *afh;
  72.   struct UFB         *ufb;
  73.   long            Arg[1],res;
  74.  
  75.   ufb = (struct UFB *) chkufb(fileno(fp));
  76.   afh = (struct FileHandle *)(ufb->ufbfh);
  77.   if ( ! IsInteractive(afh)) {
  78.     errno = ENOTTY;
  79.     return(-1);
  80.   }
  81.   mp  = ((struct FileHandle *)(BADDR(afh)))->fh_Type;
  82.   Arg[0] = 0;
  83.   res = SendPacket(mp,ACTION_SCREEN_MODE,Arg,1);
  84.   if (res == 0) {
  85.     errno = ENXIO;
  86.     return(-1);
  87.   }
  88.   return(0);
  89. }
  90.